/*
    Marv the Miner for Nokia Series 30, 40 and 60 and for selected MIDP 1.0 supported phones
    Copyright (C) 2003-2004  Digital Entertainment Europe AS (http://www.digiment.no)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
	
    If you have any questions, please contact support@digiment.no
*/
class CObject
{
	int type;
	int subtype;
	int x1,y1,x2,y2,xmin,xmax,ymin,ymax,dy,dx,k,wcnt,disp,mx,my,px,py,d,fw,fh,x,y;

	CObject nextObj; // next object for teleport

	int imageIndex = 0;
	boolean blocks[] = null;

	CObject(int type,int subtype,int x1,int y1,int x2,int y2)
	{
		this.type = type;
		this.subtype = subtype;
		this.x1 = x1;
		this.x2 = x2;
		this.y1 = y1;
		this.y2 = y2;
	}

	void init()
	{
		xmin = (x1 < x2) ? x1 : x2;
		xmax = (x1 > x2) ? x1 : x2;
		ymin = (y1 < y2) ? y1 : y2;
		ymax = (y1 > y2) ? y1 : y2;

		if (((type != Main.C_Sucker) && (type != Main.C_Slope) && (x1 > x2)) || 
			(((type == Main.C_Slope) || (type == Main.C_Lift)) && (y1 > y2)))
	    {
	    	dx = x1;
	    	x1 = x2;
	    	x2 = dx;
	    	dy = y1;
	    	y1 = y2;
	    	y2 = dy;
	    }

	    my = y2;
	    d = 2;

	    dx = x2 - x1;
		dy = y2 - y1;
		k = 0;

		if (type == Main.C_Lift)
		{
			d = 2;
		}
		if (type == Main.C_P)
		{
			imageIndex = 6 * subtype;
			k = dy * 10 / dx;
			if (Math.abs(dy * 100 / dx) % 10 >= 5) k += (dx * dy >= 0) ? 1 : -1;
			if (k == 2) imageIndex += 1;
			if (k == -2) imageIndex += 2;
			disp = (k < 0) ? k + 1 : 0;
			int size = (x2 - x1) / 10;
			if ((x2 - x1) % 10 > 0) size++;
			blocks = new boolean[size];
			for (int i = 0; i < size; i++) blocks[i] = false;
		}
		if (type == Main.C_M)
		{
			if (dx != 0)
			{
				k = dy * 10 / dx;
				if (Math.abs(dy * 100 / dx) % 10 >= 5) k += (dx * dy >= 0) ? 1 : -1;
			}
			px = mx = x1;
			py = my = y1;
			d = 2;
		}
		if (type == Main.C_Slope)
		{
			k = imageIndex = 0;
			if (dx > 0)
			{
				k = 1;
				imageIndex = 1;
			}
			if (dx < 0)
			{
				k = -1;
				imageIndex = 2;
			}
		}
	}

	void update()
	{
		if (type == Main.C_M)
		{
			if (wcnt > 0) wcnt--;
			else
			{
				if (dx != 0)
				{
					mx += d;
					my = y1 + (mx - x1) * k / 10;
					if ((mx < xmin) || (mx > xmax))
					{
						d = -d;
						wcnt = 30;
					}
				}
				else
				{
					my += d;
					if ((my < ymin) || (my > ymax))
					{
						d = -d;
						wcnt = 30;
					}
				}
				if (Main.man.curObj == this)
				{
					Main.man.new_x = Main.man.x + mx - px;
					Main.man.new_y = Main.man.y + my - py;
				}
				px = mx;
				py = my;
			}
		}
		if (type == Main.C_Lift)
		{
			if (wcnt > 0) wcnt--;
			else
			{
				my += d;
				if ((my - fh - 4 <= ymin) || (my > ymax))
				{
					d = -d;
					wcnt = 30;
				}
				if (Main.man.curObj == this)
				{
					Main.man.new_y = Main.man.y + my - py;
				}
				py = my;
			}
		}
		if (type == Main.C_Press)
		{
			my += d;
			if (ymin + 10 > my) d = 5;
			if (my >= ymax) d = -2;
			x = Main.man.x;
			y = Main.man.y;
			if ((d > 0) && (x1 - (fw >> 1) - 4 <= x) && (x1 + (fw >> 1) + 4 >= x) && (y >= my) && (y - 15 < my) && (Main.state == 0)) 
				Main.man.crash();
		}
	}
}

